<script setup>
import {getPayInfoAPI, payOrderAPI, cancelOrderAPI} from '@/apis/checkout';
import {czBalance} from '@/apis/user';
import {ref, reactive, onMounted, watch} from 'vue';
import {useRoute, useRouter} from 'vue-router';
import {useCountDown} from '@/composables/useCountDown';
import {ElMessage} from 'element-plus';

const route = useRoute(); // 路由信息
const router = useRouter(); // 路由实例
const payInfo = ref({}); // 支付信息对象
const payType = ref(0);
const ispay = ref(false);
const userBanlaceForm = reactive({
  balance: 0
});
const dialogVisible = ref(false);
// 倒计时
const {formatTime, start} = useCountDown();
// 调用接口 传入id 获取支付信息
const getOrder = async () => {
  await getPayInfoAPI(route.params.id).then((res) => {
    payInfo.value = res.data;
    // 倒计时15分钟
    start(15 * 60);
  });
};
// 监听时间
watch(formatTime, (newValue) => {
  if (newValue === '00分00秒') {
    // 取消订单
    cancelOrderAPI(payInfo.value.orderNo).then((res) => {
      ElMessage.warning("订单超时已取消");
      router.push('/');
    });
  }
});
onMounted(() => getOrder());

// 处理支付
const handlePay = async () => {
  ispay.value = true;
  await payOrderAPI({
    orderNo: payInfo.value.orderNo,
    payType: payType.value,
    isPay: ispay.value
  }).then((res) => {
    dialogVisible.value = false;
    ElMessage.success(res.message);
    router.push('/order');
  }).catch((err) => {
    console.log(err);
    ElMessage.error(err.message);
  });
};

// 处理未支付
const handleNoPay = async () => {
  await payOrderAPI({
    orderNo: payInfo.value.orderNo,
    payType: payType.value,
    isPay: ispay.value
  }).then((res) => {
    ElMessage.warning(res.message);
    dialogVisible.value = false;
    router.push('/order');
  }).catch((err) => {
    console.log(err);
  });
};

const payOrder = (type) => {
  dialogVisible.value = true;
  payType.value = type;
};

const czd = ref(false);

const cz = () => {
  czd.value = true;
};
const czConfirm = async () => {
  if (userBanlaceForm.balance <= 0) {
    ElMessage.error("充值金额必须大于零！");
    return;
  }
  await czBalance({
    balance: userBanlaceForm.balance
  }).then((res) => {
    ElMessage.success(res.message);
    czd.value = false;
  }).catch((err) => {
    console.log(err);
    ElMessage.error(err.message);
  });
};
</script>

<template>
  <div class="pay-page">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
        </div>
        <!--        <div class="amount">-->
        <!--          <span>应付总额：</span>-->
        <!--          <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span>-->
        <!--        </div>-->
      </div>
      <hr>
      <div class="info">
        <el-row>
          <el-col :span="6">订单号：</el-col>
          <el-col :span="18">
            <p>{{ payInfo.orderNo }}</p>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">收货地址：</el-col>
          <el-col :span="18">
            <p>{{ payInfo.receiveName }}</p>
            <p>{{ payInfo.receivePhone }}</p>
            <p>{{ payInfo.address }}</p>
          </el-col>
        </el-row>
        <el-row v-for="(item, index) in payInfo.products" :key="index">
          <el-col :span="6">商品{{ index }}：</el-col>
          <el-col :span="18">
            <img v-if="item.productImage" :src="'/api/images/' + item.productImage" alt="">
            <p>{{ item.productName }}</p>
          </el-col>
        </el-row>
      </div>

      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">余额不足？？</p>
        <a class="btn" href="javascript:;" @click="cz" style="margin-bottom: 20px">充值</a>

        <p class="head">选择以下支付方式付款</p>

        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;" @click="payOrder(1)"></a>
          <a class="btn alipay" href="javascript:;" @click="payOrder(2)"></a>
          <a class="btn" href="javascript:;" @click="payOrder(3)" style="margin-bottom: 20px">余额支付</a>
        </div>
      </div>
    </div>

    <el-dialog v-model="czd" title="充值" width="30%">
      <el-form :model="userBanlaceForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="充值金额">
          <el-input v-model="userBanlaceForm.balance"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="czConfirm">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="dialogVisible" title="支付确认" width="30%">
      <span>您是否完成订单?</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handlePay">已支付</el-button>
          <el-button @click="handleNoPay">未支付</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.pay-page {
  margin-top: 20px;
}

.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center; /* This will center the content horizontally */
  height: 240px;
  padding: 0 80px;
  text-align: center; /* Ensure text is centered */

  .icon {
    font-size: 80px;
    color: #1dc779;
    margin-right: 20px; /* Space between icon and text */
  }

  .tip {
    padding-left: 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center; /* Vertically center the text */

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
}

.amount {
  span {
    &:first-child {
      font-size: 16px;
      color: #999;
    }

    &:last-child {
      color: $priceColor;
      font-size: 20px;
    }
  }
}


.info {
  padding: 20px 80px;

  .el-row {
    padding: 10px 0;
  }

  img {
    width: 20px;
    height: 20px;
    margin-right: 10px;
  }

  p {
    display: inline-block;
    color: #999;
    font-size: 14px;
    text-align: right;
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }

    &.ye {
      background: url("https://hy-metaverso.oss-cn-hangzhou.aliyuncs.com/test/2024-12-23/ada695febe2e4ee89abecc59284f615e_微信截图_20241223151046.png");
    }
  }
}
</style>
